<?php

namespace app\admin\controller;
use think\Db;
use app\admin\model\UserModel;
use app\admin\model\UserType;
use think\Session;

class Fenxiao extends Base
{

    /**
     * [index 用户列表]
     * @return [type] [description]
     * @author
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['a.fen_username|a.fen_realname|a.fen_phone'] = ['like',"%" . $key . "%"];
            }
            if(isset($level)&&$level!="")
            {
                $map['a.fen_level'] = $level;
            }

            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_fenxiao')->alias('a')
                ->where($map)
                ->count();
            $lists = Db::table('up_fenxiao')->alias('a')
                ->where($map)
                ->order('fen_id desc')
                ->page($Nowpage, $limits)
                ->select();//计算总页面
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch("fenxiao/index");
    }


    /**
     * [userAdd 添加用户]
     * @return [type] [description]
     * @author
     */
    public function fenxiaoAdd()
    {
        if(request()->isPost()){
            $param = input('post.');
            if(!preg_match("/^1[3456789]\d{9}$/", $param['fen_phone'])){
                return json(['status'=>0,'msg'=>'请填写正确的手机号码']);
            }
            if(empty($param['fen_password'])){
                return json(['code' => 0,  'msg' => '密码不能为空']);
            }
            if(strlen($param['fen_password']) < 6 || strlen($param['fen_password']) > 30){
                return json(['code' => 0,  'msg' => '密码不能低于6位或高于30位']);
            }
            $have = Db::table('up_fenxiao')
                ->where('fen_username',$param['fen_username'])
                ->whereOr('fen_phone',$param['fen_phone'])
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '当前登录账号或手机号已存在']);
            }
            $param['fen_password'] = md5($param['fen_password']);
            $param['fen_time'] = date('Y-m-d H:i:s');
            $base64url = $param['fen_image'];
            $res = base64_img($base64url,true);
            if($res['code'] == 200){
                $param['fen_image'] = $res['msg'];
            }elseif($res['code'] == 100){
                return json($res);
            }
            $res2 = Db::table('up_fenxiao')->insert($param);
            if($res2){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败，请刷新重试']);
            }
        }
        return $this->fetch('fenxiao/useradd');
    }

    /**
     * checkName 验证管理员名称唯一性
     */
    public function checkName(){
        extract(input());
        $have = Db::table('up_fenxiao')->where('fen_username',$username)->find();
        if($have){
            return json(['code' => 100, 'msg' => '当前登录账号已存在']);
        }else{
            return json(['code' => 200, 'msg' => 'OK']);
        }
    }

    /**
     * [userEdit 编辑用户]
     * @return [type] [description]
     * @author
     */
    public function userEdit()
    {
        if(request()->isPost()){
            $param = input('post.');
            if(empty($param['fen_password'])){
                unset($param['fen_password']);
            }else{
                $param['fen_password'] = md5($param['fen_password']);
            }
            $base64url = $param['fen_image'];
            $res = base64_img($base64url,true);
            if($res['code'] == 200){
                $param['fen_image'] = $res['msg'];
            }elseif($res['code'] == 100){
                return json($res);
            }
            $param['fen_time'] = date('Y-m-d H:i:s');
            $res2 = Db::table('up_fenxiao')->where('fen_id',$param['fen_id'])->update($param);
            if($res2){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败，请刷新重试']);
            }
        }
        $id = input('param.id');
        $user = Db::table('up_fenxiao')->where('fen_id',$id)->find();
        $this->assign('user',$user);
        return $this->fetch("fenxiao/useredit");
    }

}